//
//  KBVistorView.swift
//  KimboWeiBoSwift
//
//  Created by kimbo on 15/12/16.
//  Copyright © 2015年 kimbo. All rights reserved.
//

import UIKit

//按钮点击协议,需要继承NSObjectProtocol
protocol KBVistorViewDelegate: NSObjectProtocol {
    //默认是必须实现的
    func registButtonDidClick()
    func loginButtonDidClick()
}

class KBVistorView: UIView {

    // MARK: 属性
    weak var delegate: KBVistorViewDelegate?
    
    // MARK: 方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        //创建UI
        setupUI()
    }

    private func setupUI() {
        
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registButton)
        addSubview(loginButton)
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        //添加约束
        /* 转轮 */
        //CenterX
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //CenterY
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        /* 遮罩 */
        //Width
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        //Top
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        //Bottom
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        /* 小房子 */
        //CenterX
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //CenterY
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        /* 文字提醒 */
        //CenterX
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //Top
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //Width
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        
        /* 注册按钮 */
        //Leading
        self.addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        //Top
        self.addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //Width
        self.addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //Height
        self.addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        /* 登录按钮 */
        //Trailing
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        //Top
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //Width
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //Height
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
    }
    
    //设置vistorView的信息
    func setupInfo(imageName: String, message: String) {
        
        iconView.image = UIImage(named: imageName)
        
        messageLabel.text = message
        
        //隐藏小房子
        homeView.hidden = true
        
        //把遮罩移到最下面
//        sendSubviewToBack(coverView)
        coverView.hidden = true
    }
    
    //转轮转动动画
    func startRotationAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = M_PI * 2
        animation.duration = 10
        animation.repeatCount = MAXFLOAT
        
        //加上这句,防止切换到其他控制再切换回来时,动画停止(动画完成时不移除)
        animation.removedOnCompletion = false
        
        iconView.layer.addAnimation(animation, forKey: nil)
    }
    
    //暂停动画
    func pauseAnimation() {
        //1记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        //2设置动画速度为0
        iconView.layer.speed = 0
        
        //3记录动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    
    //继续动画
    func resumeAniation() {
        //1 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        //2 设置动画速度为1
        iconView.layer.speed = 1
        
        //3 重置偏移时间
        iconView.layer.timeOffset = 0
        
        //4 重置开始时间
        iconView.layer.beginTime = 0
        
        //5 计算开始时间
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        //6 设置开始时间
        iconView.layer.beginTime = timeSincePause
    }
    
    //点击注册按钮,通知代理
    //用private修饰，必须要加@objc，否则系统会找不到方法而报错
    @objc private func registClick() {
        delegate?.registButtonDidClick()
    }
    
    //点击登录按钮,通知代理
    @objc private func loginClick() {
        delegate?.loginButtonDidClick()
    }
    
    // MARK: 懒加载
    /* 转轮 */
    private lazy var iconView: UIImageView = {
        
        let iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return iconView
    }()

    /* 小房子 */
    private lazy var homeView: UIImageView = {
       
        let homeView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return homeView
    }()
    
    /* 文字提醒 */
    private lazy var messageLabel: UILabel = {
       
        let messageLabel = UILabel()
        
        messageLabel.text = "关注一些人,看看这里有什么惊喜!"
        
        messageLabel.font = UIFont.systemFontOfSize(14)
        
        messageLabel.textAlignment = NSTextAlignment.Center
        
        messageLabel.textColor = UIColor.darkGrayColor()
        
        messageLabel.numberOfLines = 0
        
        messageLabel.sizeToFit()
        
        return messageLabel
    }()
    
    /* 注册按钮 */
    private lazy var registButton: UIButton = {
       
        let registButton = UIButton()
        
        registButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        registButton.setTitle("注册", forState: UIControlState.Normal)
        
        registButton.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        registButton.addTarget(self, action: "registClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return registButton
    }()
    
    /* 登录按钮 */
    private lazy var loginButton: UIButton = {
       
        let loginButton = UIButton()
        
        loginButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        loginButton.setTitle("登录", forState: UIControlState.Normal)
        
        loginButton.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        
        loginButton.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return loginButton
    }()
    
    /* 遮罩 */
    private lazy var coverView: UIImageView = {
       
        let coverView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return coverView
    }()
}
